<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const categorys = ref([

])

const dialogVisible = ref(false)

//添加分类数据模型
const categoryModel = ref({
  info: '',
  endTime: '',
  priority:1
})
//添加分类表单校验
const rules = {
  info: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
  ],
  endTime: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
  ]
}
import {articleCategoryAddService,articleCategoryListService,articleCategoryDeleteService,articleCategoryUpdateService,courseGetServier} from'@/api/article.js'
const articleCategoryList=async()=>{
  let response=await articleCategoryListService();
  if(response.success===true){
    // alert("返回成功")
    const dataArray=response.data["1"];
    categorys.value=dataArray;
  }else{
    alert('获取失败')
  }
  // categorys.value=Array.form(result.data());
}
articleCategoryList();
const addCategory=async()=>{
  let result=await articleCategoryAddService(categoryModel.value)
  if(result.success===true){
    alert('添加成功')
    dialogVisible.value=false;
    articleCategoryList();
    
  }
  else{
    alert('添加失败')
  }
}
const title=ref('')
const ID=ref({
    id:''
  })
const showDialog=(row)=>{
  dialogVisible.value=true;title.value='编辑'
  categoryModel.value.info=row.info
  categoryModel.value.endTime=row.endTime
  categoryModel.value.priority=row.priority
  //拓展
  ID.value.id=row.id
  categoryModel.value.id=row.id
}
//编辑
const updateCategory=async()=>{
  // alert(ID.value.id)
  let result=await articleCategoryUpdateService(ID.value)
  if(result.success===true){
    alert('更新成功')

  }else{
    alert('更新失败')
  }
}
import { ElMessageBox } from 'element-plus';
const deleteCategory=(row)=>{
  // alert(row.id)
  ElMessageBox.confirm(
      '你确认删除该分类信息吗？',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //用户点击了确认
        // alert(row.id)
        const temp=ref({
          id:''
        })
        temp.value.id=row.id
        let result = await articleCategoryDeleteService(temp.value)
        if(result.success===true){
          alert('删除成功')
          articleCategoryList()
        }else{
          alert('删除失败')
        }
        // ElMessage.success(result.message?result.message:'删除成功')
        //再次调用getAllCategory，获取所有文章分类
        // getAllCategory()
        // articleCategoryList()
      })
      .catch(() => {
        //用户点击了取消
        // ElMessage({
        //     type: 'info',
        //     message: '取消删除',
        // })
      })
}
const clearData=()=>{
  categoryModel.value.info='',
  categoryModel.value.endTime=''
}
const course =ref({

})
const courseGet=async()=>{
  let result=await courseGetServier();
  alert(result.data['2'][0].courseName)
  if(result.success===true){
    alert('获得成功')
  }else{
    alert('失败')
  }
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>TODOLIST</span>
        <div class="extra">
          <!-- <el-button type="primary" @click="articleCategoryList">刷新</el-button> -->
          <el-button type="primary" @click="courseGet">测试</el-button>
          <el-button type="primary" @click="dialogVisible=true;title='添加任务';clearData()">添加任务</el-button>
        </div>
      </div>
    </template>
    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="任务" prop="info"></el-table-column>
      <el-table-column label="截止日期" prop="endTime"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <!-- <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button> -->
          <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
        <el-form-item label="新的任务" prop="info">
          <el-input v-model="categoryModel.info" minlength="1" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="截止日期" prop="endTime">
          <el-input v-model="categoryModel.endTime" minlength="1" maxlength="100"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="addCategory()"> 确认 </el-button>
                    <!-- <el-button type="primary" @click="title=='添加任务'? addCategory():updateCategory()"> 确认 </el-button> -->
                </span>
      </template>

    </el-dialog>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>